#!/usr/bin/env php

<?php
// 启动文件目录
defined('ROOT') or define('ROOT', __DIR__);
//日志目录
defined('LOG') or define('LOG', ROOT . DIRECTORY_SEPARATOR . 'log');

$loaderFile = ROOT . '/vendor/autoload.php';

if(file_exists($loaderFile)){
    $loader = include_once ROOT . '/vendor/autoload.php';
    $loader->add('Thumb\\', ROOT . '/src');//这里是因为src目录是自定义的目录,不是vendor下面的目录.所以要这样加载
}else{
    die("include composer autoload.php fail\n");
}

function initCheck()
{
    if (version_compare(phpversion(), '7.0.0', '<')) {
        die("php version must >= 7.0.0");
    }
    if (version_compare(swoole_version(), '1.9.15', '<')) {
        die("swoole version must >= 1.9.15");
    }
}


function commandParser()
{
    global $argv;
    $command = $argv[1] ?? null;
    $server = $argv[2] ?? null;
    return ['command' => $command, 'server' => $server];
}

function startServer($server)
{
    global $argv;
    switch (strtolower($server)) {
        case 'http':
            {
                include ROOT.DIRECTORY_SEPARATOR.'http/HttpServer.php';
                $http = new HttpServer();
                $http->start();
                break;
            }

        default:
            {
                help($command = 'help');
            }
    }
    return;
}

function stopServer($server)
{
    global $argv;
    switch (strtolower($server)) {
        case 'http':
            {
                $pid_file = LOG . '/server.pid';
                break;
            }
        default:
            {
                help($command = 'help');
            }
    }

    if (!is_file($pid_file)) {
        echo "error: pid file {$pid_file} is not exist! \n";
        return;
    }
    $pid = intval(file_get_contents($pid_file));
    if (!swoole_process::kill($pid, 0)) {
        echo "error: pid={$pid} not exist \n";
        return;
    }
    // 发送信号，终止进程
    swoole_process::kill($pid, SIGTERM);
    // 回收master创建的子进程（manager,worker,taskworker）
    swoole_process::wait();
    //等待2秒
    $nowtime = time();
    while (true) {
        usleep(1000);
        if (!swoole_process::kill($pid, 0)) {
            echo "------------stop info------------\n";
            echo "successful: server stop at " . date("Y-m-d H:i:s") . "\n";
            echo "\n";
            @unlink($pid_file);
            break;
        } else {
            if (time() - $nowtime > 2) {
                echo "-----------stop info------------\n";
                echo "error: stop server failed. please try again \n";
                echo "\n";
                break;
            }
        }
    }
}

function commandHandler()
{
    $command = commandParser();
    if (isset($command['server']) && $command['server'] != 'help') {
        switch ($command['command']) {
            case "start":
                {
                    startServer($command['server']);
                    break;
                }
            case 'stop':
                {
                    stopServer($command['server']);
                    break;
                }
            case 'help':
            default:
                {
                    help($command['command']);
                }
        }
    } else {
        help($command['command']);
    }
}

initCheck();
commandHandler();

